﻿package {
	import gwdflash.display.FormMail;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.filters.DropShadowFilter;
	
	public class Contato extends MovieClip{
		private var shadowFilter:DropShadowFilter;
		private var library:Object;
		private var formmail:FormMail;

		public function Contato() {
			shadowFilter = new DropShadowFilter(0, 0, 0, 1, 10, 10, 1, 3, false, false, false);
		}
		
		public function preStart(_library:Object):void {
			library = _library;
			
			var pageTitle:MovieClip = new library.pageTitle;
			pageTitle.x = 525;
			this.filters = [shadowFilter];
			this.addChild(pageTitle);
			
			var pageHeaderFormat:TextFormat = new TextFormat();
			pageHeaderFormat.align = 'center';
			pageHeaderFormat.font = 'Arial';
			pageHeaderFormat.color = 0xffffff;
			pageHeaderFormat.size = 25;
			pageHeaderFormat.bold = true;
			var pageHeader:TextField = new TextField();
			pageHeader.defaultTextFormat = pageHeaderFormat;
			pageHeader.text = "Contato";
			pageHeader.width = pageTitle.width;
			pageHeader.y = 13;
			pageHeader.selectable = false;
			pageTitle.addChild(pageHeader);
			
			var contactHeaderFormat:TextFormat = new TextFormat();
			contactHeaderFormat.size = 14;
			contactHeaderFormat.color = 0x333333;
			contactHeaderFormat.align = 'center';
			contactHeaderFormat.font = 'Arial';
			contactHeaderFormat.bold = true;
			var contactHeader:TextField = new TextField();
			contactHeader.defaultTextFormat = contactHeaderFormat;
			contactHeader.text = "Contato, campos marcados com \"*\" são obrigatórios:";
			contactHeader.x = 220;
			contactHeader.y = 150;
			contactHeader.autoSize = TextFieldAutoSize.LEFT;
			this.addChild(contactHeader);
			
			var formLabels:Array = new Array("Nome", "E-mail", "Telefone", "Assunto", "Mensagem");
			var inputTypes:Array = new Array('text', 'text', 'number', 'text', 'textArea');
			formmail = new FormMail(formLabels, inputTypes, formmailAction, new library.formSendButton);
			formmail.requiredFields = ["Nome", "E-mail", "Mensagem"];
			formmail.x = 220;
			formmail.y = 180;
			formmail.scaleX = 1.3;
			formmail.scaleY = 1.3;
			formmail.failDialog = new library.formmailFailure;
			formmail.successDialog = new library.formmailSuccess;			
			this.addChild(formmail);
			
			var detailsContainer:MovieClip = new MovieClip();
			detailsContainer.graphics.beginFill(0xf5f5f5);
			detailsContainer.graphics.lineStyle(1, 0);
			detailsContainer.graphics.drawRect(0, 0, 390, 286);
			detailsContainer.graphics.endFill();
			detailsContainer.x = 690;
			detailsContainer.y = 180;
			this.addChild(detailsContainer);
			
			var detailsFormat:TextFormat = new TextFormat();
			detailsFormat.size = 20;
			detailsFormat.color = 0x555577;
			detailsFormat.align = 'left';
			detailsFormat.font = 'Arial';
			detailsFormat.bold = false;
			
			var iconEmail:MovieClip = new library.emailIcon;
			iconEmail.x = 10;
			iconEmail.y = 10;
			detailsContainer.addChild(iconEmail);
			
			var email:TextField = new TextField();
			email.defaultTextFormat = detailsFormat;
			email.text = "E-Mail: contato@gabrielguelfi.com.br";
			email.x = 60;
			email.y = 15;
			email.autoSize = TextFieldAutoSize.LEFT;
			detailsContainer.addChild(email);
			
			var iconPhone:MovieClip = new library.callIcon;
			iconPhone.x = 10;
			iconPhone.y = (detailsContainer.height - iconPhone.height) / 2;
			detailsContainer.addChild(iconPhone);
			
			var phone:TextField = new TextField();
			phone.defaultTextFormat = detailsFormat;
			phone.text = "Fone: +55(11)984-732-979";
			phone.height = phone.textHeight;
			phone.x = 60;
			phone.y = (detailsContainer.height - phone.height) / 2;;
			phone.autoSize = TextFieldAutoSize.LEFT;
			detailsContainer.addChild(phone);
			
			var iconSkype:MovieClip = new library.skypeIcon;
			iconSkype.x = 10;
			iconSkype.y = 236;
			detailsContainer.addChild(iconSkype);
			
			var skype:TextField = new TextField();
			skype.defaultTextFormat = detailsFormat;
			skype.text = "Skype: gabriel-guelfi";
			skype.x = 60;
			skype.y = 241;
			skype.autoSize = TextFieldAutoSize.LEFT;
			detailsContainer.addChild(skype);
		}
		
		public function start():void {
			if(String(library.flashVars.currentPage) == 'contato' && String(library.flashVars.mailed) == 'true'){
				formmail.showWarning(true);
			}
		}
		
		private function formmailAction(reqArgs:Object):void {
			reqArgs.formmail = 'true';
			reqArgs.currentPage = 'contato';
	
			navigateToURL(new URLRequest(URIQueryString('index.php', reqArgs)), '_self');
		}
		
		private function URIQueryString(initURL:String, vars:Object):String {
			var finalURI:String = initURL;
			var queryString:String = "?";
			
			for(var key:String in vars){
				queryString += strStandards(key) + "=" + String(vars[key] + "&");
			}
			queryString = queryString.substring(0, queryString.length - 1);
			finalURI += queryString;
			
			return finalURI;
		}
		
		private function strStandards(str:String):String{
			var arrPatterns:Array = new Array();
			
			arrPatterns.push( { pattern:/[äáàâãª]/g,  char:'a' } );
			arrPatterns.push( { pattern:/[ÄÁÀÂÃ]/g,  char:'A' } );
			arrPatterns.push( { pattern:/[ëéèê]/g,   char:'e' } );
			arrPatterns.push( { pattern:/[ËÉÈÊ]/g,   char:'E' } );
			arrPatterns.push( { pattern:/[íîïì]/g,   char:'i' } );
			arrPatterns.push( { pattern:/[ÍÎÏÌ]/g,   char:'I' } );
			arrPatterns.push( { pattern:/[öóòôõº]/g,  char:'o' } );
			arrPatterns.push( { pattern:/[ÖÓÒÔÕ]/g,  char:'O' } );
			arrPatterns.push( { pattern:/[üúùû]/g,   char:'u' } );
			arrPatterns.push( { pattern:/[ÜÚÙÛ]/g,   char:'U' } );
			arrPatterns.push( { pattern:/[ç]/g,   char:'c' } );
			arrPatterns.push( { pattern:/[Ç]/g,   char:'C' } );
			arrPatterns.push( { pattern:/[ñ]/g,   char:'n' } );
			arrPatterns.push( { pattern:/[Ñ]/g,   char:'N' } );
			arrPatterns.push( { pattern:/[\- ]/g,   char:'' } );
			
			for(var i:uint = 0; i < arrPatterns.length; i++){
				str = str.replace(arrPatterns[i].pattern, arrPatterns[i].char);
			}
		 
			return str;
		}
	}
}